package com.andy.service.impl;

import com.andy.infrastructure.util.FileUtil;
import com.andy.infrastructure.util.FreeMarkerUtil;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AbiCreateCrudService {

    @Autowired
    private AbiCreateDaoService abiCreateDaoService;

    @Autowired
    private freemarker.template.Configuration cfg;

    @Autowired
    private FreeMarkerUtil freeMarkerUtil;

    @Value("${myClasspath}")
    private String myClasspath;

    private static final String TOTLE_FILE_PATH = "D:\\autoOutput";

    //首次开发分文件夹，省去大批量的拷贝
    private static final String CONTROLLER_FILE_PATH = TOTLE_FILE_PATH+"\\controller";
    private static final String SERVICE_FILE_PATH = TOTLE_FILE_PATH+"\\service";
    private static final String SERVICE_IMPL_FILE_PATH = TOTLE_FILE_PATH+"\\service\\impl";
    private static final String REQUEST_FILE_PATH = TOTLE_FILE_PATH+"\\request";
    private static final String RESPONSE_FILE_PATH = TOTLE_FILE_PATH+"\\response";

    /**
     * 创建百威controller、出入参、service
     * @param input
     * @return
     */
    public void createAbiControllerClass(String input){
        Map<String, Object> map = abiCreateDaoService.getDbMap(input);
        //创建controller
        doCreateAbiControllerClass(map);
        //创建service
        doCreateAbiServiceClass(map);
        //创建serviceImpl
        doCreateAbiServiceImplClass(map);
        //request
        doCreateAbiRequest(map);
        //response
        doCreateAbiResponse(map);

        //弹出框体
        FileUtil.popupDir(TOTLE_FILE_PATH);
    }


    /**
     * 创建controller
     * @param map
     */
    private void doCreateAbiControllerClass(Map<String, Object> map){
        String ftlName = "abi_template_controller.ftl";
        Template t = null;
        try {
            t = cfg.getTemplate(ftlName);
            // 输出到控制台
            StringWriter writer = new StringWriter();
            t.process(map, writer);
            String poStr = writer.toString();

            //生成文件
            String fileName = (String)map.get("targetName_Iii")+"Controller.java";
            String filePath = CONTROLLER_FILE_PATH;
            //创建文件
            FileUtil.createFile(fileName,poStr,filePath);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建service接口
     * @param map
     */
    private void doCreateAbiServiceClass(Map<String, Object> map){
        String ftlName = "abi_template_service.ftl";
        Template t = null;
        try {
            t = cfg.getTemplate(ftlName);
            // 输出到控制台
            StringWriter writer = new StringWriter();
            t.process(map, writer);
            String poStr = writer.toString();

            //生成文件
            String fileName = (String)map.get("targetName_Iii")+"Service.java";
            String filePath = SERVICE_FILE_PATH;
            //创建文件
            FileUtil.createFile(fileName,poStr,filePath);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void doCreateAbiServiceImplClass(Map<String, Object> map){
        String ftlName = "abi_template_serviceImpl.ftl";
        Template t = null;
        try {
            t = cfg.getTemplate(ftlName);
            // 输出到控制台
            StringWriter writer = new StringWriter();
            t.process(map, writer);
            String poStr = writer.toString();

            //生成文件
            String fileName = (String)map.get("targetName_Iii")+"ServiceImpl.java";
            String filePath = SERVICE_IMPL_FILE_PATH;
            //创建文件
            FileUtil.createFile(fileName,poStr,filePath);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void doCreateAbiRequest(Map<String, Object> map){
        doCreateOneAbiRequest("abi_template_request_add.ftl","Add",map);
        doCreateOneAbiRequest("abi_template_request_update.ftl","Update",map);
        doCreateOneAbiRequest("abi_template_request_list.ftl","List",map);
        doCreateOneAbiRequest("abi_template_request_get.ftl","Get",map);
        doCreateOneAbiRequest("abi_template_request_enable.ftl","Enable",map);
        doCreateOneAbiRequest("abi_template_request_disable.ftl","Disable",map);
    }

    private void doCreateOneAbiRequest(String ftlName,String filePrefix,Map<String, Object> map){
        Template t = null;
        try {
            t = cfg.getTemplate(ftlName);
            // 输出到控制台
            StringWriter writer = new StringWriter();
            t.process(map, writer);
            String poStr = writer.toString();

            //生成文件
            String fileName = filePrefix+(String)map.get("targetName_Iii")+"Req.java";
            String filePath = REQUEST_FILE_PATH;
            //创建文件
            FileUtil.createFile(fileName,poStr,filePath);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void doCreateAbiResponse(Map<String, Object> map){
        doCreateOneAbiResponse("abi_template_response_list.ftl","List",map);
        doCreateOneAbiResponse("abi_template_response_get.ftl","Get",map);
    }

    private void doCreateOneAbiResponse(String ftlName,String filePrefix,Map<String, Object> map){
        Template t = null;
        try {
            t = cfg.getTemplate(ftlName);
            // 输出到控制台
            StringWriter writer = new StringWriter();
            t.process(map, writer);
            String poStr = writer.toString();

            //生成文件
            String fileName = filePrefix+(String)map.get("targetName_Iii")+"Res.java";
            String filePath = RESPONSE_FILE_PATH;
            //创建文件
            FileUtil.createFile(fileName,poStr,filePath);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
